package com.example.cheeseerpadmin.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.sys.common.*;
import com.example.cheeseerpadmin.sys.entity.SysPermission;
import com.example.cheeseerpadmin.sys.entity.SysUser;
import com.example.cheeseerpadmin.sys.service.SysPermissionService;
import com.example.cheeseerpadmin.sys.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    //注入用于查询permission的service层
    @Autowired
    public SysPermissionService sysPermissionService;

    @RequestMapping("/loadPermissionLeftTreeJson")
    public DataGridView loadPermissionLeftTreeJson(PermissionVo permissionVo) {
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constant.TYPE_MENU);
        List<SysPermission> list = sysPermissionService.list(queryWrapper);
        List<TreeNode> treeNodes = new ArrayList<>();
        for (SysPermission sysPermission : list) {
            TreeNode treeNode = new TreeNode(sysPermission.getId(), sysPermission.getPid(), sysPermission.getTitle(), sysPermission.getOpen() == 1);
            treeNodes.add(treeNode);
        }
        return new DataGridView(treeNodes);
    }

    /**
     * 模糊查询，查询的类型得是permission
     *
     * @param permissionVo
     * @return
     */
    @RequestMapping("/loadAllPermission")
    public DataGridView loadAllPermission(PermissionVo permissionVo) {
        Page<SysPermission> page = new Page<>(permissionVo.getPage(), permissionVo.getLimit());
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constant.TYPE_PERMISSION);
        queryWrapper.like(StringUtils.isNotBlank(permissionVo.getTitle()), "title", permissionVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(permissionVo.getPercode()), "percode", permissionVo.getPercode());
        //下面不需要用or()方法连接，只需要查询到跟菜单相关联的权限就可以了
        queryWrapper.eq(permissionVo.getId() != null, "pid", permissionVo.getId());
        Page<SysPermission> sysPermissionPage = sysPermissionService.page(page, queryWrapper);
        return new DataGridView(sysPermissionPage.getTotal(), sysPermissionPage.getRecords());
    }

    /**
     * 添加权限
     *
     * @param permissionVo
     * @return
     */
    @RequestMapping("/addPermission")
    public ResultObj addPermission(PermissionVo permissionVo) {
        try {
            permissionVo.setType("permission");
            sysPermissionService.save(permissionVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 获取排序码
     *
     * @return
     */
    @RequestMapping("/loadPermissionMaxOrderNum")
    public Map<String, Object> loadPermissionMaxOrderNum() {
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        Page<SysPermission> page = new Page<>(1, 1);
        List<SysPermission> list = sysPermissionService.page(page, queryWrapper).getRecords();
        if (list.size() > 0) {
            SysPermission sysPermission = list.get(0);
            Integer ordernum = sysPermission.getOrdernum() + 1;
            map.put("value", ordernum);
            return map;
        } else {
            map.put("value", 1);
            return map;
        }
    }

    /**
     * 更新权限
     *
     * @param permissionVo
     * @return
     */
    @RequestMapping("/updatePermission")
    public ResultObj updatePermission(PermissionVo permissionVo) {
        try {
            sysPermissionService.updateById(permissionVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @RequestMapping("/deletePermissionById")
    public ResultObj deletePermissionById(Integer id) {
        try {
            sysPermissionService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
